﻿using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.HumanAffairs.Promotions;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.HumanAffairs.Promotions
{
    public class PromotionRepository : DapperDbContextRepository, IPromotionRepository
    {
        public PromotionRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 转正办理——查询分页显示
        /// <summary>
        /// 转正办理——查询分页显示
        /// </summary>
        /// <param name="PromotionName"></param>
        /// <param name="DeptId"></param>
        /// <param name="PositionId"></param>
        /// <param name="EntryTime"></param>
        /// <param name="ApplyTime"></param>
        /// <param name="State"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<PromotionDto>> GetPromotionPageShow(string PromotionName, int DeptId, int PositionId, DateTime? EntryTime, DateTime? ApplyTime, int State, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<PromotionDto> response = new PageResponse<PromotionDto>();
            var sql = "select a.*,b.DeptName as BName,c.PositionName as CName,d.DictionaryName as DName from Promotion a join Department b on a.DeptId=b.Id join Position c on a.PositionId=c.Id join Dictionary d on a.State=d.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(PromotionName))
            {
                sql += $" and a.PromotionName like '%" + PromotionName + "%'";
            }
            if (DeptId > 0)
            {
                sql += $" and a.DeptId='{DeptId}'";
            }
            if (PositionId > 0)
            {
                sql += $" and a.PositionId='{PositionId}'";
            }
            if (EntryTime != null)
            {
                sql += $" and a.EntryTime ='{EntryTime}'";
            }
            if (ApplyTime != null)
            {
                sql += $" and a.ApplyTime ='{ApplyTime}'";
            }
            if (State > 0)
            {
                sql += $" and a.State='{State}'";
            }
            var list = await this.QueryAsync<PromotionDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<PromotionDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 转正办理——逻辑删除
        /// <summary>
        /// 转正办理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeletePromotion(int id)
        {

            var sql = $"update Promotion set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 转正办理——批量删除
        /// <summary>
        /// 转正办理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchPromotion(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Promotion set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 转正办理——编辑
        /// <summary>
        /// 转正办理——编辑
        /// </summary>
        /// <param name="promotionDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdatePromotion(PromotionDto promotionDto)
        {
            var sql = $"update Promotion set PromotionNumber='{promotionDto.PromotionNumber}',PromotionName='{promotionDto.PromotionName}',DeptId='{promotionDto.DeptId}',PositionId='{promotionDto.PositionId}',SuperiorManager='{promotionDto.SuperiorManager}',EntryTime='{promotionDto.EntryTime}',ApplyTime='{promotionDto.ApplyTime}',CreateTime='{promotionDto.CreateTime}',State='{promotionDto.State}',Summarize='{promotionDto.Summarize}',Idea='{promotionDto.Idea}',IsDelete='{promotionDto.IsDelete}' where Id='{promotionDto.Id}'";
            return await ExecuteAsync(sql, promotionDto);
        }
        #endregion

        #region 转正办理——添加
        /// <summary>
        /// 转正办理——添加
        /// </summary>
        /// <param name="promotionDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddPromotion(PromotionDto promotionDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Promotion values('{id}','{promotionDto.PromotionName}','{promotionDto.DeptId}','{promotionDto.PositionId}','{promotionDto.SuperiorManager}',getdate(),'{promotionDto.ApplyTime}',getdate(),'{promotionDto.State}','{promotionDto.Summarize}',0',{promotionDto.Idea}')";
            return await ExecuteAsync(sql, promotionDto);
        }
        #endregion
    }
}
